package cn.techaction.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.springframework.stereotype.Repository;


import cn.techaction.dao.ActionUserDao;
import cn.techaction.pojo.User;

@Repository
public class ActionUserDaoImpl implements ActionUserDao{
	@Resource
	private QueryRunner queryRunner;
	
	@Override
	public int checkUserByAccount(String account) {
	
		String sql="select count(account) as num from users where account=?";
		try {
			List<Long> rs=queryRunner.query(sql, new ColumnListHandler<Long>("num"),account);
<<<<<<< HEAD
			return rs.size()>0?rs.get(0).intValue():0;//濡傛灉鏁版嵁搴撲腑鏈夎繖涓处鍙凤紝杩斿洖闀垮害銆傚惁鍒欓暱搴︿负闆�
=======
<<<<<<< HEAD
			return rs.size()>0?rs.get(0).intValue():0;//濡傛灉鏁版嵁搴撲腑鏈夎繖涓处鍙凤紝杩斿洖闀垮害銆傚惁鍒欓暱搴︿负闆�
=======
			return rs.size()>0?rs.get(0).intValue():0;//数据库中若有数据则返回长度 否则返回零
>>>>>>> 7692d496e2cdc9fb600af15df0f9004717978403
>>>>>>> 24179ae77518350a17bcc562c200035028b0a954
			
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("鏉ヨ繖浜嗭紵");
			return 0;
		}
	}
	@Override
	public User findUserByAccountAndPassword(String account, String password) {
		// TODO Auto-generated method stub
		String sql="select * from users where account=? and password=?";
		try {
			return queryRunner.query(sql,new BeanHandler<User>(User.class),account,password);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}

	}
	
	@Override
	public int checkUserByEmail(String email) {
		String sql="select count(account) as num from users where email=?";
		try {
			List<Long> rs=queryRunner.query(sql, new ColumnListHandler<Long>("num"),email);
			return rs.size()>0?rs.get(0).intValue():0;
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public int checkUserByPhone(String phone) {
		// TODO Auto-generated method stub
		String sql="select count(account) as num from users where phone=?";
		try {
			List<Long> rs=queryRunner.query(sql, new ColumnListHandler<Long>("num"),phone);
			return rs.size()>0?rs.get(0).intValue():0;
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public int insertUser(User user) {
		// TODO Auto-generated method stub
		String sql="insert into users(account,password,email,phone,question,asw,role,create_time,update_time)"
				+"values(?,?,?,?,?,?,?,?,?)";
		Object[] params= {user.getAccount(),user.getPassword(),user.getEmail(),user.getPhone(),user.getQuestion(),
				user.getAsw(),user.getRole(),user.getCreate_time(),user.getUpdate_time()};
		try {
			return queryRunner.update(sql,params);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public User findUserByAccount(String account) {
		// TODO Auto-generated method stub
		String sql="select * from users where account=?";
		try {
			return queryRunner.query(sql,new BeanHandler<User>(User.class),account);
			
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		

	}
	//鏍￠獙瀵嗕繚绛旀
	@Override
	public int checkUserAnswer(String account, String question, String asw) {
		// TODO Auto-generated method stub
		String sql ="select count(account) as num from users where account = ? and question = ? and asw = ?";
		try {
			List<Long> rs=queryRunner.query(sql, new ColumnListHandler<Long>("num"),account,question,asw);
			return rs.size()>0?rs.get(0).intValue():0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		
	}
	@Override
	public User findUserById(Integer userId) {
		// TODO Auto-generated method stub
		String sql="select * from users where id =?";
		try {
			return queryRunner.query(sql, new BeanHandler<User>(User.class),userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	@Override
	public int updateUserInfo(User user) {
		// TODO Auto-generated method stub
		String sql="update users set update_time=? , password= ? , email= ? ,phone= ? ,"
				+ "question= ?,asw= ?,name = ?,age= ? ,sex= ?,create_time= ?,account= ?,role= ?,del= ? where id=?";
		List<Object> params=new ArrayList<>();
		params.add(user.getUpdate_time());
		params.add(user.getPassword());
		params.add(user.getEmail());
		params.add(user.getPhone());
		params.add(user.getQuestion());
		params.add(user.getAsw());
		params.add(user.getName());
		params.add(user.getAge());
		params.add(user.getSex());
		params.add(user.getCreate_time());
		params.add(user.getAccount());
		params.add(user.getRole());
		params.add(user.getDel());
		params.add(user.getId());
		try {
			return queryRunner.update(sql,params.toArray());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		
	}
	@Override
	public int checkPassword(String account, String password) {
		// TODO Auto-generated method stub
		String sql="select count(account) as num from users where account = ? and password = ?";
		try {
			List<Long> rs=queryRunner.query(sql, new ColumnListHandler<Long>("num"),account,password);
			return rs.size()>0?rs.get(0).intValue():0;
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

}
